package com.api.gbhg.lsq.action;

import com.api.gbhg.lbj.action.SourceAction;
import com.api.gbhg.liwenjun.VersionFlowUtil;
import com.api.gbhg.zengping.common.CommonDao;

import weaver.conn.RecordSet;
import weaver.conn.RecordSetTrans;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.math.BigDecimal;
import java.util.*;

/**
 * @author lsq
 * @version 1.0
 * @description: TODO 同步报销数据到报销数据记录表(主表)
 * @date 2021/4/20 16:52
 */
public class MainReimbursementDataAction implements Action {
    private static final Logger logger = LoggerFactory.getLogger("gbhg");
    private final SourceAction sou = new SourceAction();

    //@Override
    public String execute(RequestInfo requestInfo) {

        logger.info("进入主表报销后同步报销uf_bxsjjl表---");

        //请求id
        String requestid = requestInfo.getRequestid();
        RecordSet del = new RecordSet();
        del.execute("delete from uf_bxsjjl where lcid="+requestid);
        RecordSetTrans rst = new RecordSetTrans();
        rst.setAutoCommit(false);
        //当前流程表名
        String billTableName = requestInfo.getRequestManager().getBillTableName();

        //根据请求id查询主表信息
        RecordSet rs = CommonDao.selectByRequestId(new RecordSet(), billTableName, requestid);
        Map<String, String> list = new HashMap<>();
        int i = 0;
        boolean b2 = true;
        String ysxm = "";
        if (rs.next()) {

            //流程类型 0:主表同步,1:明细表同步
            String lclx = "0";
            //流程id:requestid
            //预算项目id
            ysxm = rs.getString("ysxm");
            //申请报销金额
            String sqbxje = rs.getString("bxje").equals("")?rs.getString("sqbxje"):rs.getString("bxje");
            //同意报销金额
            String tybxje = rs.getString("tybxje");
            //部门经济分类
            String bmjjzcfl = rs.getString("bmjjzcfl");
            //政府经济分类
            String zfjjzcfl = rs.getString("zfjjzcfl");
            String bccxjkje = rs.getString("bccxjkje").equals("")?"0":rs.getString("bccxjkje");
            //是否已提交  0:未提交,1:已提交 默认为未提交
            String sfytj = "1";


            list.put("lclx", lclx);//流程类型
            list.put("lcid", requestid);//流程id
            list.put("ysxm", ysxm);//预算项目id
            list.put("sqbxje", sqbxje);//申请报销金额
            list.put("tybxje", tybxje);//同意报销金额
            list.put("bmjjfl", bmjjzcfl);//部门经济支出分类
            list.put("zfjjfl", zfjjzcfl);//政府经济支出分类
            list.put("sfytj", sfytj);//是否已提交
            list.put("cxje", bccxjkje);//冲销金额

            ++i;
            logger.info("主表申请报销金额:" + sqbxje + "同意报销金额:" + tybxje);

        }
        // 存储主表报销数据到经费来源表
        b2 = storageMainDateToBaoXiaoSj(list,rst);
        if (!b2) {
            rst.rollback();
            logger.info("============================提交节点后保存报销数据出现错误(主表)======================================");
            requestInfo.getRequestManager().setMessagecontent("提交节点后保存报销数据出现错误(主表)");
            requestInfo.getRequestManager().setMessageid("1");
            return FAILURE_AND_CONTINUE;
        }
        rst.commit();
        return Action.SUCCESS;
    }


    public boolean storageMainDateToBaoXiaoSj(Map<String, String> list,RecordSetTrans rst) {
        boolean flag = true;
        Set set = list.keySet();
        List<String> listField = new ArrayList<String>(set);

        // 报销数据
        Collection<String> valueCollection = list.values();
        List<String> valueList = new ArrayList<>(valueCollection);
        // 存储数据到报销数据记录表
        String uf_baoxiaoshujusql = VersionFlowUtil.getInsertSql(listField, "uf_bxsjjl");
        try {
            logger.info("同步报销流程sql---->" + uf_baoxiaoshujusql);
            rst.executeUpdate(uf_baoxiaoshujusql, valueList.toArray());
        } catch (Exception e) {
            e.printStackTrace();
            logger.info("同步报销流程sql异常---->" + uf_baoxiaoshujusql + valueCollection);
            logger.info("errorsql==" + e.toString());
            flag = false;
        }
        return flag;
    }
}
